import Vue from 'vue'
import Router from 'vue-router'
import FrontPage from "../components/FrontPage";
import AddOrder from "../components/Order/AddOrder"
import OrderManager from "../components/Order/OrderManager";
import OrderProductManager from "../components/Order/OrderProductManager";
import ChangeOrder from "../components/Order/ChangeOrder";
import PendingOrderManager from "../components/Order/PendingOrderManager";
import MyPickingOrderManager from "../components/Order/MyPickingOrderManager";
import VendorsManager from "../components/Vendors/VendorsManager";
import ChangeVendors from "../components/Vendors/ChangeVendors";
import AddVendors from "../components/Vendors/AddVendors";
import AddIo from "../components/IO/AddIo";
import IoData from "../components/IO/IoData";
import ProductManager from "../components/Product/ProductManager";
import ChangeProduct from "../components/Product/ChangeProduct";
import AddProduct from "../components/Product/AddProduct";
import Login from "../components/User/Login";
import Main from "../components/PageStructure/Main";
import Register from "../components/User/Register";
import MyWaitDeliverOrderData from "../components/Order/MyWaitDeliverOrderData";
import RackData from "../components/Rack/RackData";
import ChangePassword from "../components/User/ChangePassword";
import Analysis from "../components/Analysis/Analysis";
import AddRack from "../components/Rack/AddRack";
import ChangeRack from "../components/Rack/ChangeRack";
import UserData from "../components/User/UserData";
import ChangeUser from "../components/User/ChangeUser";
import AddUser from "../components/User/AddUser";

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/main",
    },
    {
      path:"/login",
      name:"login",
      component:Login,
      meta: {
        title: "登录"
      }
    },
    {
      path:"/register",
      name:"register",
      component:Register,
      meta: {
        title: "注册"
      }
    },
    {
      path:"/main",
      component:Main,
      redirect: "/main/frontData",
      children:[
        {
          path:"frontData",
          component:FrontPage,
          meta: {
            title: "首页"
          }
        },
        {
          path:"addOrder",
          type: "order",
          component:AddOrder,
          meta: {
            title: "添加订单"
          }
        },
        {
          path: "orderData",
          type: "order",
          component: OrderManager,
          meta: {
            title: "订单详情"
          }
        },
        {
          path: "orderContent/:id",
          type: "order",
          component: OrderProductManager,
          meta: {
            title: "订单内容"
          }
        },
        {
          path: "changeOrder/:id",
          type: "order",
          component: ChangeOrder,
          meta: {
            title: "修改订单"
          }
        },
        {
          path: "pendingOrderData",
          type: "order",
          component: PendingOrderManager,
          meta: {
            title: "待处理订单"
          }
        },
        {
          path: "myPickingOrderData",
          type: "order",
          component: MyPickingOrderManager,
          meta: {
            title: "拣货中订单"
          }
        },
        {
          path: "vendorsData",
          type: "vendors",
          component: VendorsManager,
          meta: {
            title: "供应商详情"
          }
        },
        {
          path: "changeVendors/:id",
          type: "vendors",
          component: ChangeVendors,
          meta: {
            title: "修改供应商"
          }
        },
        {
          path: "addVendors",
          type: "vendors",
          component: AddVendors,
          meta: {
            title: "添加供应商"
          }
        },
        {
          path: "addIo",
          type: "io",
          component: AddIo,
          meta: {
            title: "添加入库记录"
          }
        },
        {
          path: "ioData",
          type: "io",
          component: IoData,
          meta: {
            title: "出入库记录详情"
          }
        },
        {
          path: "productData",
          type: "product",
          component: ProductManager,
          meta: {
            title: "商品详情"
          }
        },
        {
          path: "changeProduct/:index",
          type: "product",
          component: ChangeProduct,
          meta: {
            title: "修改商品"
          }
        },
        {
          path: "addProduct",
          type: "product",
          component: AddProduct,
          meta: {
            title: "添加商品"
          }
        },
        {
          path: "myWaitDeliverOrderData",
          type: "order",
          component: MyWaitDeliverOrderData,
          meta: {
            title: "待发货订单"
          }
        },
        {
          path: "rackData",
          type: "rack",
          component: RackData,
          meta: {
            title: "货架详情"
          }
        },
        {
          path: "changePassword",
          component: ChangePassword,
          meta: {
            title: "修改密码"
          }
        },
        {
          path: "analysis",
          component: Analysis,
          meta: {
            title: "数据分析"
          }
        }, {
          path: "addRack",
          type: "rack",
          component: AddRack,
          meta: {
            title: "添加货架"
          }
        }, {
          path: "changeRack/:id",
          type: "rack",
          component: ChangeRack,
          meta: {
            title: "修改货架"
          }
        }, {
          path: "userData",
          type: "user",
          component: UserData,
          meta: {
            title: "用户详情"
          }
        }, {
          path: "changeUser/:id",
          type: "user",
          component: ChangeUser,
          meta: {
            title: "修改用户"
          }
        }, {
          path: "addUser",
          type: "user",
          component: AddUser,
          meta: {
            title: "添加用户"
          }
        }
      ]
    },
  ]
})

router.beforeEach((to, from, next) => {
  if(sessionStorage.getItem("repoUserInfo") || localStorage.getItem("repoUserInfo") || to.name === 'login' || to.name === 'register'){
    if (to.meta.title) {
      document.title = "Depot仓储管理系统 | " + to.meta.title
    }
    next();
  }else {
    if (to.meta.title) {
      document.title = "Depot仓储管理系统 | " + to.meta.title
    }
    next({
      name: 'login',
      query: {
        redirect: to.path
      }
    });
  }
})

export default router
